package com.github.distributionmessage.thread;

import com.github.distributionmessage.config.IntegrationConfiguration;
import com.github.distributionmessage.utils.CommonUtils;
import lombok.extern.slf4j.Slf4j;

import java.io.File;
import java.util.concurrent.ExecutorService;

@Slf4j
public class WriteFileThread implements Runnable {

    private static ExecutorService executorService;

    private String dir;

    private Object message;

    public WriteFileThread(String dir, Object message) {
        this.dir = dir;
        this.message = message;
    }

    @Override
    public void run() {
        long startTime = System.nanoTime();
        IntegrationConfiguration.CACHE_QUEUE.poll();
        CommonUtils.getDistributionMessageGateway().writeToFile(new File(dir), this.message);
        log.info("cache size [" + IntegrationConfiguration.CACHE_QUEUE.size() + "] write message to dir[" + this.dir + "] use["
            + ((double)(System.nanoTime() - startTime) / 1000000.0) + "]ms");
    }

    public static ExecutorService getExecutorService() {
        return executorService;
    }

    public static void setExecutorService(ExecutorService executorService) {
        WriteFileThread.executorService = executorService;
    }
}
